#include "jsoncpp/json/json.h"
#include <iostream>
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/writer.h>
using namespace std;

int main() {
    Json::Value root;
    root["name"] = "张三";
    root["sex"] = "男";
    root["age"] = "18";
    // 两种写入方式，
    // 第一种，紧凑型，写在一起
    // Json::FastWriter writer;
    // std::string s = writer.write(root); // {"age":"18","name":"\u5f20\u4e09","sex":"\u7537"}
    // 第二种，美观型
    // Json::StyledWriter writer;
    // std::string s = writer.write(root);
    // cout << s << endl;
    // 嵌套写入
    Json::Value val;
    val["tel"] = "10086";
    val["籍贯"] = "江西";
    root["info"] = val;
    // string s = root.toStyledString();
    // cout << s << endl;
    Json::StyledWriter writer;
    std::string s = writer.write(root);
    cout << s << endl;

    // 识别汉字
    Json::StreamWriterBuilder builder;
    builder["emitUTF8"] = true;   // ✅ 关键设置：不转义 UTF-8 中文
    string jsonStr = Json::writeString(builder, root);
    cout << jsonStr << endl;
    return 0;
}